import time

from selenium.webdriver.common.by import By

from common.Utils import BlogDriver

class BlogLogin:
    url=""
    driver=""
    def __init__(self):
        self.url="http://8.137.19.140:9090/blog_login.html"
        self.driver=BlogDriver.driver
        self.driver.get(self.url)

    #成功登录的测试用例
    def BlogLoginSucTest(self):
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()

        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()

        #如果能找到昵称，说明登录正常
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3")
        #添加屏幕截图
        BlogDriver.getScreenShot()
        #返回登录页面
        self.driver.back()

        #换用不同的参数测试
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        # 如果能找到昵称，说明登录正常
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")
        #添加屏幕截图
        BlogDriver.getScreenShot()

    #异常登录的测试用例
    def BlogLoginFailTest(self):
        #用户名和密码为空的情况
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        #出现弹窗，需要强制等待着
        time.sleep(2)
        alert=self.driver.switch_to.alert
        alert.accept()

        #密码错误情况
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("111111")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        # 出现弹窗，需要强制等待着
        time.sleep(2)
        alert = self.driver.switch_to.alert
        alert.accept()
        # 添加屏幕截图
        BlogDriver.getScreenShot()



